// **************************************************
// * CDKColors.cpp: generate a visual map of the CDK color identifiers
// *
// * Lucas Gonze <address@hidden>
// * 10/20/02
// **************************************************

extern "C"
{
#include <cdk/cdk.h>
}
#include <sstream>
using namespace std;

#include "terminal.h"
const int max_colors = 256;
char* txt[max_colors];

class CDKColors {
  protected:
    WINDOW *cursesWin;
    CDKSCREEN *cdkscreen;

    void init_screen(){

      cdkscreen = initCDKScreen(cursesWin);
      //initCDKColor();
      for(int c = 0; c < max_colors; ++c) {
        stringstream s;
        s << "</" << c << ">" << c << "\n\0";
        char* cstr = new char[s.str().size()];
        memcpy(cstr, s.str().c_str(), s.str().size());
        txt[c] = cstr;
      }
      for(int c = 0; c < max_colors; ++c) {
        newCDKLabel (cdkscreen, (c / 32) * 6, c % 32, txt + c, 1, FALSE, FALSE);
      }

    }

  public:
    CDKSWINDOW *swindow; 

    CDKColors(){
      cursesWin = initscr();
      init_screen();
      refreshCDKScreen (cdkscreen);
    }

    void recalc_window(){

      if( !cdkscreen )
        return;

      // reinitialize the screen to match new coordinates
      destroyCDKScreen (cdkscreen);
      endCDK();
      init_screen();

      // redraw to pickup changes
      refreshCDKScreen(cdkscreen);

    }

    void wait_for_any_key(){

      while(true){
        switch( wgetch (cursesWin) ){
          case KEY_RESIZE:
            recalc_window();
            break;
          default:
            return;
        }
      }
    }

    ~CDKColors(){
      destroyCDKScreen(cdkscreen);
      delwin (cursesWin);
      endCDK();
    }

};

CDKColors *testWin = NULL;

int main (int argc, char **argv){
  auto t = Terminal::create();
  testWin = new CDKColors();
  testWin->wait_for_any_key();  
  delete testWin;
}

